package visual;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.Serializable;
import java.util.Vector;

import main.GameDef;

public class Piece implements GameDef, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8147426416142249925L;
	public Vector<Block>blocks;
	public boolean isVertical;
	public int pieceType;
	
	public Piece(int type, Point pos, boolean vertical){
		pieceType = type;
		blocks = createBlocks(pos, vertical);
	}
	
	public void paint(Graphics g){
		for(int i = 0; i < blocks.size(); i++){
			blocks.get(i).paint(g);
		}
	}
	
	public Rectangle pieceArea(){
		Rectangle area = blocks.get(0).area;
		
		for(int i = 1; i < blocks.size(); i++){
			area.add(blocks.get(i).area);
		}
		
		return area;
	}
	
	public Vector<Block> createBlocks(Point pos, boolean vertical){
		int vectorSize = 0;
		int pieceWidth = 0;
		isVertical = vertical;
		Vector<Block> temp = new Vector<Block>(0);

		switch(pieceType){
			case ID_INTERIOR:
			case ID_HOUSE:
				vectorSize = 1;
				pieceWidth = 1;
				break;
			case ID_CATAPULT:
				vectorSize = 3;
				pieceWidth = 1;
				break;
			case ID_CANNON:
				vectorSize = 6;
				pieceWidth = 2;
				break;
			case ID_ARMORY:
				vectorSize = 9;
				pieceWidth = 3;
				break;
		}
		pos.translate(-(pos.x % BLOCK_WIDTH), -(pos.y % BLOCK_HEIGHT));
		for(int i = 0; i < vectorSize; i++){
			Point blockPos = pos.getLocation();
			if(isVertical){
				blockPos.translate((i % pieceWidth) * BLOCK_WIDTH, (i / pieceWidth) * BLOCK_HEIGHT);
			} else {
				blockPos.translate((i / pieceWidth) * BLOCK_WIDTH, (i % pieceWidth) * BLOCK_HEIGHT);	
			}
			temp.add(new Block(pieceType, blockPos));
		}
		
		return temp;
	}
}
